import pymysql


class DB:
    # 创建连接对象
    def getConnection(self):
        conn = pymysql.Connect(host="localhost",
                               port=3306,
                               db="test",
                               user="root",
                               password="root",
                               charset="utf8")
        cls = conn.cursor()
        return conn, cls

    #查询，删除，更新
    def executeUpdate(self, sql, args=None):
        conn, cls = self.getConnection()
        rows = cls.execute(sql, args)
        conn.commit()
        conn.close()
        return rows

    #查询
    def executeQuery(self, sql, args=None):
        conn, cls = self.getConnection()
        cls.execute(sql, args)
        result = cls.fetchall()
        conn.close()
        return result